package edu.mobbuzz.facade;

import edu.mobbuzz.bean.Message;
import edu.mobbuzz.storage.MessageRecordStore;
import edu.mobbuzz.storage.OutboxRecordStore;
import java.util.Date;
import java.util.Vector;

public class MessageFacade {
    private MessageRecordStore messageRS;
    private OutboxRecordStore outboxRS;

    private Vector messageArray;
    private Vector outboxArray;
    
    public MessageRecordStore getMessageRS() {
        if(messageRS==null){
           messageRS = new MessageRecordStore();
        }
        return messageRS;
    }

    public void setMessageRS(MessageRecordStore messageRS) {
        this.messageRS = messageRS;
    }

    public OutboxRecordStore getOutboxRS() {
        if(outboxRS==null){
           outboxRS = new OutboxRecordStore();
        }
        return outboxRS;
    }

    public void setOutboxRS(OutboxRecordStore outboxRS) {
        this.outboxRS = outboxRS;
    }

    public void init(){
        if(getMessageRS().getNbMessage()==0){
            Message message = new Message();
            message.setType(0);
            message.setDate(new Date().toString());
            message.setDescription("Aksi menolak UU BHP kembali terjadi di Yogyakarta. Puluhan mahasiswa Yogyakarta menggelar aksi di utara kampus Universitas Islam Negeri (UIN) Sunan Kalijaga");
            message.setFrom("news@mobbuzz.com");
            message.setLink("http://www.detiknews.com/read/2008/12/27/135906/1059979/10/tolak-uu-bhp,-mahasiswa-yogya-kembali-turun-ke-jalan");
            message.setPubdate(new Date().toString());
            message.setRead(false);
            message.setSource("http://rss.detik.com/index.php/detikcom");
            message.setTitle("Tolak UU BHP, Mahasiswa Yogya Kembali Turun Ke Jalan");
            getMessageRS().addMessage(message);            

            Message message3 = new Message();
            message3.setType(0);
            message3.setDate(new Date().toString());
            message3.setDescription("Aksi menolak UU BHP kembali terjadi di Yogyakarta. Puluhan mahasiswa Yogyakarta menggelar aksi di utara kampus Universitas Islam Negeri (UIN) Sunan Kalijaga");
            message3.setFrom("news@mobbuzz.com");
            message3.setLink("http://www.detiknews.com/read/2008/12/27/135906/1059979/10/tolak-uu-bhp,-mahasiswa-yogya-kembali-turun-ke-jalan");
            message3.setPubdate(new Date().toString());
            message3.setRead(false);
            message3.setSource("http://rss.detik.com/index.php/detikcom");
            message3.setTitle("BBBBBBBBBBBBBBBBBBBBBBBBB");
            getMessageRS().addMessage(message3);

            Message message4 = new Message();
            message4.setType(0);
            message4.setDate(new Date().toString());
            message4.setDescription("Aksi menolak UU BHP kembali terjadi di Yogyakarta. Puluhan mahasiswa Yogyakarta menggelar aksi di utara kampus Universitas Islam Negeri (UIN) Sunan Kalijaga");
            message4.setFrom("news@mobbuzz.com");
            message4.setLink("http://www.detiknews.com/read/2008/12/27/135906/1059979/10/tolak-uu-bhp,-mahasiswa-yogya-kembali-turun-ke-jalan");
            message4.setPubdate(new Date().toString());
            message4.setRead(false);
            message4.setSource("http://rss.detik.com/index.php/detikcom");
            message4.setTitle("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC");
            getMessageRS().addMessage(message4);

            Message message5 = new Message();
            message5.setType(0);
            message5.setDate(new Date().toString());
            message5.setDescription("Aksi menolak UU BHP kembali terjadi di Yogyakarta. Puluhan mahasiswa Yogyakarta menggelar aksi di utara kampus Universitas Islam Negeri (UIN) Sunan Kalijaga");
            message5.setFrom("news@mobbuzz.com");
            message5.setLink("http://www.detiknews.com/read/2008/12/27/135906/1059979/10/tolak-uu-bhp,-mahasiswa-yogya-kembali-turun-ke-jalan");
            message5.setPubdate(new Date().toString());
            message5.setRead(false);
            message5.setSource("http://rss.detik.com/index.php/detikcom");
            message5.setTitle("Tolak UU BHP, Mahasiswa Yogya Kembali Turun Ke Jalan");
            getMessageRS().addMessage(message5);

            Message message6 = new Message();
            message6.setType(1);
            message6.setDate(new Date().toString());
            message6.setDescription("Aksi menolak UU BHP kembali terjadi di Yogyakarta. Puluhan mahasiswa Yogyakarta menggelar aksi di utara kampus Universitas Islam Negeri (UIN) Sunan Kalijaga");
            message6.setFrom("news@mobbuzz.com");
            message6.setLink("http://www.detiknews.com/read/2008/12/27/135906/1059979/10/tolak-uu-bhp,-mahasiswa-yogya-kembali-turun-ke-jalan");
            message6.setPubdate(new Date().toString());
            message6.setRead(false);
            message6.setSource("http://rss.detik.com/index.php/detikcom");
            message6.setTitle("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ");
            getOutboxRS().addMessage(message6);

            Message message2 = new Message();
            message2.setType(0);
            message2.setDate(new Date().toString());
            message2.setDescription("Aksi menolak UU BHP kembali terjadi di Yogyakarta. Puluhan mahasiswa Yogyakarta menggelar aksi di utara kampus Universitas Islam Negeri (UIN) Sunan Kalijaga");
            message2.setFrom("news@mobbuzz.com");
            message2.setLink("http://www.detiknews.com/read/2008/12/27/135906/1059979/10/tolak-uu-bhp,-mahasiswa-yogya-kembali-turun-ke-jalan");
            message2.setPubdate(new Date().toString());
            message2.setRead(false);
            message2.setSource("http://rss.detik.com/index.php/detikcom");
            message2.setTitle("AAAAAAAAAAAAAAAAAAAAAAAA");
            getOutboxRS().addMessage(message2);

        }
    }
    public Vector displayOutboxMessageList() {
        outboxArray = new Vector();
        //init();
        if(getOutboxRS().readRSMessage()){
            for(int i=0;i<getOutboxRS().getNbMessage();i++){
                
                    outboxArray.addElement(new Message(getOutboxRS().messageArr[i].getRecId(),
                                                        getOutboxRS().messageArr[i].getFrom(),
                                                        getOutboxRS().messageArr[i].getSource(),
                                                        getOutboxRS().messageArr[i].getTitle(),
                                                        getOutboxRS().messageArr[i].getDescription(),
                                                        getOutboxRS().messageArr[i].getLink(),
                                                        getOutboxRS().messageArr[i].getPubdate(),
                                                        getOutboxRS().messageArr[i].getDate(),
                                                        getOutboxRS().messageArr[i].getType(),
                                                        getOutboxRS().messageArr[i].isRead()));

               
            }
        }else{
            System.out.println("RS Message Gagal dibuka");
        }


        return outboxArray;
    }

    public Vector displayInboxMessageList() {
        messageArray = new Vector();
        //init();
        if(getMessageRS().readRSMessage()){
            for(int i=0;i<getMessageRS().getNbMessage();i++){
                
                messageArray.addElement(new Message(getMessageRS().messageArr[i].getRecId(),
                                                    getMessageRS().messageArr[i].getFrom(),
                                                    getMessageRS().messageArr[i].getSource(),
                                                    getMessageRS().messageArr[i].getTitle(),
                                                    getMessageRS().messageArr[i].getDescription(),
                                                    getMessageRS().messageArr[i].getLink(),
                                                    getMessageRS().messageArr[i].getPubdate(),
                                                    getMessageRS().messageArr[i].getDate(),
                                                    getMessageRS().messageArr[i].getType(),
                                                    getMessageRS().messageArr[i].isRead()));

               
            }
        }else{
            System.out.println("RS Message Gagal dibuka");
        }


        return messageArray;
    }
    
	public void deleteMessage(int index) {
        getMessageRS().deleteMessage(index);
	}
	 
	public void addMessage(Message message) {
        getMessageRS().addMessage(message);
	}

    public void updateMessage (int index, Message message) {
        getMessageRS().updateMessage(index, message);
    }

    //outbox
    public void deleteOutbox(int index) {
        getOutboxRS().deleteMessage(index);
	}

	public void addOutbox(Message message) {
        System.out.println("date : " + message.getDate());
        System.out.println("description : " + message.getDescription());
        System.out.println("from : " + message.getFrom());
        System.out.println("link : " + message.getLink());
        System.out.println("pub date : " + message.getPubdate());
        System.out.println("source : " + message.getSource());
        System.out.println("title : " + message.getTitle());
        getOutboxRS().addMessage(message);
	}

    public void updateOutbox (int index, Message message) {
        getOutboxRS().updateMessage(index, message);
    }
	 
}
 
